package com.inspur.myapplication.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.inspur.myapplication.R;
import com.inspur.myapplication.entity.UserBean;
import com.inspur.myapplication.util.ShoppingDbHelper;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Administrator on 2020/11/26.
 */

public class GoodsDao {
    private Context context;

    public GoodsDao(Context context){
        this.context=context;
    }

    public ArrayList queryGoods(String name,String category){
        ArrayList arrayList=new ArrayList();
        ShoppingDbHelper shoppingDbHelper=new ShoppingDbHelper(context);
        SQLiteDatabase sqLiteDatabase=shoppingDbHelper.getReadableDatabase();
        try {
            Cursor cursor=sqLiteDatabase.query("goodstb",new String[]{"goodsid","goodstitle","price","photo"},"category=?",new String[]{category},null,null,null);
            while (cursor.moveToNext()){
                String goodsid=cursor.getString(cursor.getColumnIndex("goodsid"));
                String goodstitle=cursor.getString(cursor.getColumnIndex("goodstitle"));
                String price=cursor.getString(cursor.getColumnIndex("price"));
                String photo=cursor.getString(cursor.getColumnIndex("photo"));
                HashMap hashMap=new HashMap();
                hashMap.put("goodsName",goodstitle);
                hashMap.put("goodsPrice",price);
                hashMap.put("goodsImg",photo);
                arrayList.add(hashMap);
            }
            cursor.close();

        }catch (Exception e){
            Log.e(GoodsDao.class.toString(),e.toString());
        }finally {
            sqLiteDatabase.close();
        }

        return arrayList;
    }
}
